const express = require("express");
const Router = express.Router();
const fs = require("fs");
const path = require("path");
const multer = require("multer");
Router.use(express.static(path.join(__dirname,"../../public")));
// 商品文件夹

const mkdirp = require("mkdirp");
const moment = require("moment");

const nowDate = moment().format("YYYY-MM-DD");

const upload = (urlPath) => {
  const storage = multer.diskStorage({
    destination: async (re, file, cb) => {
      await mkdirp(path.join(`./public/${urlPath}/${nowDate}`));
      cb(null, path.join(`./public/${urlPath}/${nowDate}`));
    },
    filename: (req, file, cb) => {
      let extname = path.extname(file.originalname);
      let fileName = path.parse(file.originalname).name; // 获取文件名称
      cb(null, `${fileName}-${Date.now()}${extname}`);
    },
  });
  return multer({ storage: storage });
};

module.exports = upload;
